package com.ruoyi.project.members.order.service.impl;

import com.ruoyi.project.members.order.domain.Order;
import com.ruoyi.project.members.order.mapper.OrderMapper;
import com.ruoyi.project.members.order.service.OrderService;
import com.ruoyi.project.user.company.domain.Company;
import com.ruoyi.project.user.company.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.soap.Addressing;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CompanyService companyService;


    @Override
    public List<Order> findOrderByList(Order order, String cname) {
        List<Order> orders = orderMapper.selectOrderList(order);
        Company com = new Company();
        if(cname != null && cname != ""){
            for(Order o: orders) {
                com.setCompanyName(cname);
                List<Company> all = companyService.findAll(com);
                for (Company c : all) {
                    o.setCompanyName(c.getCompanyName());
                    if (c.getCompanyName().equals(o.getCompanyName())){
                        List<Order> list = new ArrayList<>();
                        list.add(o);
                        return list;
                    }
                }
            }
        }else {
            for(Order o: orders){
                System.out.println(o.getOrderCeremonydate());
                Long orderUserId = o.getOrderUserId();
                com.setCompanyUserId(orderUserId);
                List<Company> all = companyService.findAll(com);
                for (Company c:all){
                    String companyName = c.getCompanyName();
                    o.setCompanyName(companyName);
                }
            }
        }

        return orders;
    }

    @Override
    public Order findOrderById(Integer orderId) {
        return orderMapper.selectOrderById(orderId);
    }

    @Override
    public List<Order> export() {
        return orderMapper.selectALL();
    }
}
